package com.dc.free.module.synchronization_common_peson.controller;

import java.util.List;

import com.dc.free.module.synchronization_common_peson.db.model.SynchronizationCommonPeson;
import com.dc.free.module.synchronization_common_peson.service.SynchronizationCommonPesonService;
import com.dc.free.plugin.SynchronizationCommonPesonScanUtil;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dc.free.common.base.BaseController;

/**
 * Generated by dyci, do not modify this file.
 */
public class SynchronizationCommonPesonController extends BaseController {
	
	public void query() {
		String request = getPara("request");
		if(StrKit.isBlank(request)) request = getParameters().toJSONString();
		String id = HashKit.generateSaltForSha256();
		SynchronizationCommonPeson  synchronizationCommonPeson = new SynchronizationCommonPeson()
				.setCode("person")
				.setId(id)
				.setRequest(request)
				.setStatus(0);
		synchronizationCommonPeson.save();
		
		try {
			SynchronizationCommonPesonScanUtil.putAwaiter(id, request);
			renderJson(SynchronizationCommonPeson.dao.findById(id).getResponse());
		} catch (InterruptedException e) {
			renderJson(e.getMessage());
			e.printStackTrace();
		} finally {
			SynchronizationCommonPeson.dao.deleteById(id);
		}
		
	}
	
	public void querySingle() {
		String request = getPara("request");
		if(StrKit.isBlank(request)) request = getParameters().toJSONString();
		String id = HashKit.generateSaltForSha256();
		SynchronizationCommonPeson  synchronizationCommonPeson = new SynchronizationCommonPeson()
				.setCode("person")
				.setId(id)
				.setRequest(request)
				.setStatus(0);
		synchronizationCommonPeson.save();
		
		try {
			SynchronizationCommonPesonScanUtil.putAwaiter(id, request);
			JSONArray arr = JSON.parseObject(SynchronizationCommonPeson.dao.findById(id).getResponse()).getJSONArray("data");
			if(arr.size() == 0)
				renderJson(new JSONObject());
			else
				renderJson(arr.getString(0));
		} catch (InterruptedException e) {
			renderJson(e.getMessage());
			e.printStackTrace();
		} finally {
			SynchronizationCommonPeson.dao.deleteById(id);
		}
		
	}
	
	public void index() {
		render("scan-information.html");
	}
	
	public void record() {
		renderJson(SynchronizationCommonPesonScanUtil.scan.getRecords());
	}
	
	public void awaiter() {
		renderJson(JSON.toJSONString(SynchronizationCommonPesonScanUtil.scan.getAwaiters()));
	}
	
	public void unlock() {
		String unionCode = getPara("unionCode");
		SynchronizationCommonPesonScanUtil.scan.unlock(unionCode);
		renderNull();
	}
															
	public void insert() {
		SynchronizationCommonPeson synchronizationCommonPeson = getModel(SynchronizationCommonPeson.class, "synchronization_common_peson");
		boolean flag = SynchronizationCommonPesonService.insert(synchronizationCommonPeson);
		if(flag) renderOk(new SynchronizationCommonPeson().setId(synchronizationCommonPeson.getId()));
		else renderError();
	}
	
	public void update() {
		SynchronizationCommonPeson synchronizationCommonPeson = getModel(SynchronizationCommonPeson.class, "synchronization_common_peson");
		boolean flag = SynchronizationCommonPesonService.update(synchronizationCommonPeson) > 0;
		if(flag) renderOk();
		else renderError();
	}
	
	public void save() {
		SynchronizationCommonPeson synchronizationCommonPeson = getModel(SynchronizationCommonPeson.class, "synchronization_common_peson");
		boolean flag = SynchronizationCommonPesonService.save(synchronizationCommonPeson);
		if(flag) renderOk(new SynchronizationCommonPeson().setId(synchronizationCommonPeson.getId()));
		else renderError();
	}
	
	public void delete() {
		SynchronizationCommonPeson synchronizationCommonPeson = getModel(SynchronizationCommonPeson.class, "synchronization_common_peson");
		boolean flag = SynchronizationCommonPesonService.delete(synchronizationCommonPeson) > 0;
		if(flag) renderOk();
		else renderError();
	}
	
	public void get() {
		SynchronizationCommonPeson synchronizationCommonPeson = getModel(SynchronizationCommonPeson.class, "synchronization_common_peson");
		synchronizationCommonPeson = SynchronizationCommonPesonService.get(synchronizationCommonPeson);
		renderOk(synchronizationCommonPeson);
	}
	
	public void list() {
		SynchronizationCommonPeson synchronizationCommonPeson = getModel(SynchronizationCommonPeson.class, "synchronization_common_peson");
		List<SynchronizationCommonPeson> synchronizationCommonPesons = SynchronizationCommonPesonService.list(synchronizationCommonPeson);
		renderOk(synchronizationCommonPesons);
	}
	
	public void paginate() {
		SynchronizationCommonPeson synchronizationCommonPeson = getModel(SynchronizationCommonPeson.class, "synchronization_common_peson");
		int pageSize = getParaToInt("pageSize", 10);
		int pageNumber = getParaToInt("pageNumber", 1);
		SynchronizationCommonPesonService.paginate(synchronizationCommonPeson, pageSize, pageNumber);
	}
	
	public void repeat() {
		SynchronizationCommonPeson synchronizationCommonPeson = getModel(SynchronizationCommonPeson.class, "synchronization_common_peson");
		boolean repeat  = SynchronizationCommonPesonService.repeat(synchronizationCommonPeson);
		renderOk(repeat);
	}
	
}